﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;

using Common.Model;
using Common.Error;
using OpenAPI.Model.API;
using OpenAPI.BLL.API;
using OpenAPI.Web.Mvc.Helper;
using OpenAPI.Web.Mvc.Filter;

namespace OpenAPI.Web.Mvc.Controllers
{
    [ExceptionFilter]
    [AuthFilter]
    public class MethodController : Controller
    {
        #region Method

        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult Add()
        {
            IList<MethodCategoryInfo> categories = OpenAPI.BLL.API.MethodManager.GetCategories();

            MethodCategoryInfo category = new MethodCategoryInfo();
            category.CATEGORY_CD = "";
            category.CATEGORY_NAME = "";
            category.CATEGORY_DESC = "";
            category.CATEGORY_ORDER = "";
            categories.Insert(0, category);

            ViewData["METHOD_CATEGORY"] = new SelectList(categories, "CATEGORY_CD", "CATEGORY_NAME");

            return View();
        }

        [ManagerAuthFilter]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AjaxAdd()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            MethodInfo method = new MethodInfo();
            method.CATEGORY_CD = Utility.StringHelper.GetPost("category_cd").Trim();
            method.METHOD_NAME = Utility.StringHelper.GetPost("method_name").Trim();
            method.METHOD_TEXT = Utility.StringHelper.GetPost("method_text").Trim();
            method.METHOD_DESC = Utility.StringHelper.GetPost("method_desc");
            method.METHOD_RETURN = Utility.StringHelper.GetPost("method_return");
            method.METHOD_ORDER = Utility.StringHelper.GetPost("method_order").Trim();
            method.USED_YN = Utility.StringHelper.GetPost("used_yn").Trim();

            if (string.IsNullOrEmpty(method.CATEGORY_CD))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category code can not empty.");
            }
            else if (string.IsNullOrEmpty(method.METHOD_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method name can not empty.");
            }
            else if (string.IsNullOrEmpty(method.METHOD_TEXT))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method function can not empty.");
            }
            else if (string.IsNullOrEmpty(method.METHOD_ORDER))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method display order can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.Add(method, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult Mod(string id)
        {
            string method_name = id;
            MethodInfo method = OpenAPI.BLL.API.MethodManager.GetMethod(method_name);

            IList<MethodCategoryInfo> categories = OpenAPI.BLL.API.MethodManager.GetCategories();
            MethodCategoryInfo category = new MethodCategoryInfo();
            category.CATEGORY_CD = "";
            category.CATEGORY_NAME = "";
            category.CATEGORY_DESC = "";
            category.CATEGORY_ORDER = "";
            categories.Insert(0, category);

            ViewData["METHOD_CATEGORY"] = new SelectList(categories, "CATEGORY_CD", "CATEGORY_NAME", method.CATEGORY_CD);

            return View(method);
        }

        [ManagerAuthFilter]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AjaxMod()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            MethodInfo method = new MethodInfo();
            method.CATEGORY_CD = Utility.StringHelper.GetPost("category_cd").Trim();
            method.METHOD_NAME = Utility.StringHelper.GetPost("method_name").Trim();
            method.METHOD_TEXT = Utility.StringHelper.GetPost("method_text").Trim();
            method.METHOD_DESC = Utility.StringHelper.GetPost("method_desc");
            method.METHOD_RETURN = Utility.StringHelper.GetPost("method_return");
            method.METHOD_ORDER = Utility.StringHelper.GetPost("method_order").Trim();
            method.USED_YN = Utility.StringHelper.GetPost("used_yn").Trim();

            if (string.IsNullOrEmpty(method.CATEGORY_CD))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category code can not empty.");
            }
            else if (string.IsNullOrEmpty(method.METHOD_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method name can not empty.");
            }
            else if (string.IsNullOrEmpty(method.METHOD_TEXT))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method function can not empty.");
            }
            else if (string.IsNullOrEmpty(method.METHOD_ORDER))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method display order can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.Mod(method, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult List()
        {
            IList<MethodInfo> methods = OpenAPI.BLL.API.MethodManager.GetMethods("");

            return View(methods);
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxDel()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            string method_name = Utility.StringHelper.GetPost("method_name");

            if (string.IsNullOrEmpty(method_name))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method name can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.Delete(method_name, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Method Arguments

        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult SignArguments(string id)
        {
            string method_name = id;
            MethodInfo method = OpenAPI.BLL.API.MethodManager.GetMethod(method_name);
            ViewData["SignArguments"] = OpenAPI.BLL.API.MethodManager.GetSignArguments(method_name);

            return View(method);
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxAddSignArgument()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            MethodSignArgumentInfo argument = new MethodSignArgumentInfo();
            MethodSignArgumentInfoPK pk = new MethodSignArgumentInfoPK();
            pk.METHOD_NAME = Utility.StringHelper.GetPost("method_name").Trim();
            pk.ARGUMENT_NAME = Utility.StringHelper.GetPost("argument_name").Trim().ToLower();
            argument.MethodSignArgumentPK = pk;
            argument.ARGUMENT_VALUE = Utility.StringHelper.GetPost("argument_value").Trim();
            argument.ARGUMENT_DATATYPE = Utility.StringHelper.GetPost("argument_datatype").Trim();
            argument.ARGUMENT_DESC = Utility.StringHelper.GetPost("argument_desc");

            if (string.IsNullOrEmpty(argument.MethodSignArgumentPK.METHOD_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method name can not empty.");
            }
            else if (string.IsNullOrEmpty(argument.MethodSignArgumentPK.ARGUMENT_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Argument name can not empty.");
            }
            else if (string.IsNullOrEmpty(argument.ARGUMENT_DATATYPE))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Argument datatype can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.AddSignArgument(argument, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxDeleteSignArgument()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            string method_name = Utility.StringHelper.GetPost("method_name").Trim();
            string argument_name = Utility.StringHelper.GetPost("argument_name").Trim().ToLower();

            if (string.IsNullOrEmpty(method_name))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method name can not empty.");
            }
            else if (string.IsNullOrEmpty(argument_name))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Argument name can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.DeleteSignArgument(method_name, argument_name, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult UnSignArguments(string id)
        {
            string method_name = id;
            MethodInfo method = OpenAPI.BLL.API.MethodManager.GetMethod(method_name);
            ViewData["UnSignArguments"] = OpenAPI.BLL.API.MethodManager.GetUnSignArguments(method_name);

            return View(method);
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxAddUnSignArgument()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            MethodUnSignArgumentInfo argument = new MethodUnSignArgumentInfo();
            MethodUnSignArgumentInfoPK pk = new MethodUnSignArgumentInfoPK();
            pk.METHOD_NAME = Utility.StringHelper.GetPost("method_name").Trim();
            pk.ARGUMENT_NAME = Utility.StringHelper.GetPost("argument_name").Trim().ToLower();
            argument.MethodUnSignArgumentPK = pk;
            argument.ARGUMENT_VALUE = Utility.StringHelper.GetPost("argument_value").Trim();
            argument.ARGUMENT_DATATYPE = Utility.StringHelper.GetPost("argument_datatype").Trim();
            argument.ARGUMENT_DESC = Utility.StringHelper.GetPost("argument_desc");

            if (string.IsNullOrEmpty(argument.MethodUnSignArgumentPK.METHOD_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method name can not empty.");
            }
            else if (string.IsNullOrEmpty(argument.MethodUnSignArgumentPK.ARGUMENT_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Argument name can not empty.");
            }
            else if (string.IsNullOrEmpty(argument.ARGUMENT_DATATYPE))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Argument datatype can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.AddUnSignArgument(argument, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxDeleteUnSignArgument()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            string method_name = Utility.StringHelper.GetPost("method_name").Trim();
            string argument_name = Utility.StringHelper.GetPost("argument_name").Trim().ToLower();

            if (string.IsNullOrEmpty(method_name))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Method name can not empty.");
            }
            else if (string.IsNullOrEmpty(argument_name))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Argument name can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.DeleteUnSignArgument(method_name, argument_name, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        #endregion

        #region Method Category

        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult CategoryAdd()
        {
            return View();
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxCategoryAdd()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            MethodCategoryInfo category = new MethodCategoryInfo();
            category.CATEGORY_CD = Utility.StringHelper.GetPost("category_cd").Trim();
            category.CATEGORY_NAME = Utility.StringHelper.GetPost("category_name").Trim();
            category.CATEGORY_DESC = Utility.StringHelper.GetPost("category_desc");
            category.CATEGORY_ORDER = Utility.StringHelper.GetPost("category_order");

            if (string.IsNullOrEmpty(category.CATEGORY_CD))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category code can not empty.");
            }
            else if (string.IsNullOrEmpty(category.CATEGORY_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category name can not empty.");
            }
            else if (string.IsNullOrEmpty(category.CATEGORY_ORDER))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category display order can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.AddCategory(category, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [ManagerAuthFilter]
        [HttpGet]
        public ActionResult CategoryMod(string id)
        {
            string category_cd = id;
            MethodCategoryInfo category = OpenAPI.BLL.API.MethodManager.GetCategory(category_cd);

            return View(category);
        }

        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxCategoryMod()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            MethodCategoryInfo category = new MethodCategoryInfo();
            category.CATEGORY_CD = Utility.StringHelper.GetPost("category_cd").Trim();
            category.CATEGORY_NAME = Utility.StringHelper.GetPost("category_name").Trim();
            category.CATEGORY_DESC = Utility.StringHelper.GetPost("category_desc");
            category.CATEGORY_ORDER = Utility.StringHelper.GetPost("category_order");

            if (string.IsNullOrEmpty(category.CATEGORY_CD))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category code can not empty.");
            }
            else if (string.IsNullOrEmpty(category.CATEGORY_NAME))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category name can not empty.");
            }
            else if (string.IsNullOrEmpty(category.CATEGORY_ORDER))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category display order can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.ModifyCategory(category, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [ManagerAuthFilter]
        public ActionResult CategoryList()
        {
            IList<MethodCategoryInfo> categories = OpenAPI.BLL.API.MethodManager.GetCategories();

            return View(categories);
        }


        [ManagerAuthFilter]
        [HttpPost]
        public ActionResult AjaxCategoryDel()
        {
            ResultInfo result = new ResultInfo();

            ErrorType error = null;
            bool result_stat = false;

            string category_cd = Utility.StringHelper.GetPost("category_cd");

            if (string.IsNullOrEmpty(category_cd))
            {
                result_stat = false;
                error = ErrorHelper.NewError("", "Category code can not empty.");
            }
            else
            {
                result_stat = OpenAPI.BLL.API.MethodManager.DeleteCategory(category_cd, out error);

                if (result_stat)
                {
                    error = null;
                }
            }

            //处理返回结果
            if (result_stat)
            {
                result.FLAG = true;
                result.CODE = "";
                result.DESC = "Success";
                result.DATA = null;
            }
            else
            {
                result.FLAG = false;
                result.CODE = (error == null) ? "" : error.ERROR_CODE;
                result.DESC = (error == null) ? "" : error.ERROR_MESSAGE;
                result.DATA = null;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }//
}//
